package com.rain.spit.service;

import com.rain.spit.dao.SpitDao;
import com.rain.spit.pojo.Spit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import utils.IdWorker;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 吐槽的业务层
 * @auther 德哲
 * @date 2019/4/28 14:18.
 */
@Service
public class SpitService {

    @Autowired
    private SpitDao spitDao;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询所有
     * @return
     */
    public List<Spit> findAll(){
        return spitDao.findAll();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public Spit findById(String id){
        return spitDao.findById(id).get();
    }

    /**
     * 新增吐槽
     * @param spit
     */
    public void save(Spit spit){
        spit.set_id(String.valueOf(idWorker.nextId()));
        spit.setComment(0);
        spit.setVisits(0);
        spit.setThumbup(0);
        spit.setPublishtime(new Date());
        spit.setUserid("1");
        if (!StringUtils.isEmpty(spit.getParentid()))
        {
            Spit spit1 = spitDao.findById(spit.getParentid()).get();
            spit1.setComment(spit.getComment()+1);
            spitDao.save(spit1);
        }
        spitDao.save(spit);
    }

    /**
     * 更新吐槽
     * @param spit
     */
    public void update(Spit spit){
        spitDao.save(spit);
    }

    /**
     * 删除id
     * @param id
     */
    public void delete(String id){
        spitDao.deleteById(id);
    }

    /**
     * 根据父级id查询吐槽
     * @param parentid
     * @param page
     * @param size
     * @return
     */
    public Page<Spit> findByParentid(String parentid,int page,int size){
        PageRequest pageRequest = PageRequest.of(page-1,size);
        return spitDao.findByParentid(parentid,pageRequest);
    }

    /**
     * 吐槽点赞
     */
    public void updateThumbup(String id){
        //要求一个月之内不能重复点赞。
        //获取用户id
        String userid= "11";
        //去redis缓存中查看是否有过点赞的行为
        Object val = redisTemplate.opsForValue().get("spit_" + userid + "_" + id);
        if (val != null) {
            throw new RuntimeException("请不要重复点赞");
        }
        //用于确定更新那条数据
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.inc("thumbup",1);
        mongoTemplate.updateFirst(query,update,"spit");
        //30天有效期
        redisTemplate.opsForValue().set("spit_" + userid + "_" + id,"thumbup",30, TimeUnit.DAYS);
    }
}
